package com.zyk.leetcode;

import com.zyk.util.ListUtil;

/**
 * @author zhangsan
 * @date 2021/4/26 17:08
 */
public class C21 {


    public static ListNode mergeTwoLists(ListNode l1, ListNode l2) {
        ListNode head = new ListNode(-1);
        ListNode cur = head;

        while (l1 != null && l2 != null) {
            if(l1.val <= l2.val) {
                cur.next = l1;
                l1 = l1.next;
            }else {
                cur.next = l2;
                l2 = l2.next;
            }
            cur = cur.next;
        }
        cur.next = l1 == null ? l2 : l1;
        return head.next;
    }


    // for test
    public static void main(String[] args) {
        int[] arr1 = {1, 2, 3};
        ListNode l1 = ListUtil.generateList(arr1);

        int[] arr2 = {1, 3, 4};
        ListNode l2 = ListUtil.generateList(arr2);

        ListNode newHead = mergeTwoLists(l1, l2);
        while (newHead!= null) {
            System.out.println(newHead.val);
            newHead = newHead.next;
        }
    }

}
